package com.book.service.impl;

import com.book.api.ReaderApi;
import com.book.pojo.CollectRanking;
import com.book.mapper.CollectRankingDao;
import com.book.pojo.Shoucang;
import com.book.service.CollectRankingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxy
 * @since 2023-10-13 18:30:46
 */
@Service
public class CollectRankingServiceImpl extends ServiceImpl<CollectRankingDao, CollectRanking> implements CollectRankingService {
    @Autowired
    private CollectRankingDao collectRankingDao;
    @Autowired
    private ReaderApi readerApi;

    @Override
    public List<CollectRanking> CollectRanking() {
        List<Shoucang> shoucangs = readerApi.CollectRanking();
        CollectRanking collectRanking = new CollectRanking();
        for (Shoucang shoucang : shoucangs) {
            collectRankingDao.deleteBybid(shoucang.getBookId());
            collectRanking.setBookId(shoucang.getBookId());
            collectRanking.setNumber(shoucang.getNumber());
            collectRankingDao.insert(collectRanking);
        }

        return collectRankingDao.selectList(null);
    }

    @Override
    public void deleteBybid(Long bid) {
        collectRankingDao.deleteBybid(bid);
    }

    @Override
    public void addCollectRanking(CollectRanking collectRanking) {
        collectRankingDao.insert(collectRanking);
    }
}
